using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Cards.GUI;
using Cards.Objects;

namespace Cards.Util
{
    public class CheckRule_VNPocker
    {
        public static INS_Player s_currentWinPigPlayer = null;
        public static INS_Player s_currentLosePigPlayer = null;
        public static int s_currentAmount = 0;

        public static Boolean CheckPlayedCard(ArrayList _array)
        {
            if (_array.Count < 3)
            {
                switch (_array.Count)
                {
                    case 0:
                        return false;
                    case 1:
                        return true;
                    case 2:
                        if (!CheckACouple(_array))
                        {
                            return false;
                        }
                        break;
                }
            }
            else if(_array.Count <= 12){
                if(_array.Count % 2 == 0) //4,6,8,10,12 la bai
                {
                    if (!Check4CardsOfKind(_array) && !CheckPine(_array) && !CheckSortedArray(_array))
                    {
                        return false;
                    }
                }
                else //3,5,7,9,11 la bai
                {
                    if (_array.Count == 3)
                    {
                        if (!Check3CardsOfKind(_array) && !CheckSortedArray(_array))
                        {
                            return false;
                        }
                    }
                    else if(!CheckSortedArray(_array))
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }

            return true;
        }

        public static Boolean CheckCardGreater(ArrayList _arrCurrentCards, ArrayList _arrPreviousCards, 
                                            INS_Player _previousPlayer, INS_Player _currentPlayer)
        {
            if (_arrPreviousCards.Count == 0)
            {
                return true;
            }

            //Kiem tra so quan bai danh ra
            if (!CheckPigs(_arrPreviousCards) && !CheckPine(_arrPreviousCards) && !Check4CardsOfKind(_arrPreviousCards))
            {
                if (_arrCurrentCards.Count != _arrPreviousCards.Count)
                {
                    return false;
                }
            }

            if (_arrPreviousCards.Count == 1)
            {
                if (_arrCurrentCards.Count == 1)
                {
                    if (CheckPigs(_arrPreviousCards))
                    {
                        if (((INS_Card)_arrCurrentCards[0]).CARD.CARDTYPE == Constants.HAIBICH)
                        {
                            //INS_Stage.sLoseRate++;
                        }
                        else if (((INS_Card)_arrCurrentCards[0]).CARD.CARDTYPE == Constants.HAICHUON)
                        {
                            INS_VNPockerStage.sLoseRate++;
                        }
                        if (((INS_Card)_arrCurrentCards[0]).CARD.CARDTYPE == Constants.HAIRO)
                        {
                            INS_VNPockerStage.sLoseRate += 2;
                        }
                        else if (((INS_Card)_arrCurrentCards[0]).CARD.CARDTYPE == Constants.HAICO)
                        {
                            INS_VNPockerStage.sLoseRate += 3;
                        }
                    }

                    if (((INS_Card)_arrCurrentCards[0]).CARD.CARDTYPE
                        > ((INS_Card)_arrPreviousCards[0]).CARD.CARDTYPE)
                    {
                        return true;
                    }
                }
                else
                {
                    //chat 1 heo
                    if (Check4CardsOfKind(_arrCurrentCards) || CheckPine(_arrCurrentCards))
                    {
                        if (((INS_Card)_arrPreviousCards[0]).CARD.CARDTYPE == Constants.HAIBICH)
                        {
                            _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            INS_VNPockerStage.sLoseRate *= 2;
                        }
                        else if (((INS_Card)_arrPreviousCards[0]).CARD.CARDTYPE == Constants.HAICHUON)
                        {
                            _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            INS_VNPockerStage.sLoseRate *= 2;
                        }
                        else if (((INS_Card)_arrPreviousCards[0]).CARD.CARDTYPE == Constants.HAIRO)
                        {
                            INS_VNPockerStage.sLoseRate *= 2;
                            _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            }
                        else if (((INS_Card)_arrPreviousCards[0]).CARD.CARDTYPE == Constants.HAICO)
                        {
                            INS_VNPockerStage.sLoseRate *= 2;
                            _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                        }
                        _previousPlayer.PLAYER.NOTETOSUBTRACT = _previousPlayer.PLAYER.NOTETOSUBTRACT + "Chat Heo;";
                        _currentPlayer.PLAYER.NOTETOADD = _currentPlayer.PLAYER.NOTETOADD + "Chat Heo;";
                        return true;
                    }
                }
            }
            else if (_arrPreviousCards.Count == 2)
            {
                //Bat doi
                if (_arrCurrentCards.Count == 2)
                {
                    if (ArrayUtil.Max(((INS_Card)_arrCurrentCards[0]).CARD.CARDTYPE, ((INS_Card)_arrCurrentCards[1]).CARD.CARDTYPE)
                        > ArrayUtil.Max(((INS_Card)_arrPreviousCards[0]).CARD.CARDTYPE, ((INS_Card)_arrPreviousCards[1]).CARD.CARDTYPE))
                    {
                        if(CheckPigs(_arrCurrentCards))
                        {
                            for(int i =0;i<2;i++)
                            {
                                INS_VNPockerStage.sLoseRate += (((INS_Card)_arrCurrentCards[i]).CARD.CARDTYPE - 48);
                            }
                        }
                        return true;
                    }
                }
                //Bat heo bang doi thong hoac tu quy
                else
                {
                    if (Check4CardsOfKind(_arrCurrentCards) || (CheckPine(_arrCurrentCards) && _arrCurrentCards.Count > 6))
                    {
                        _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                        _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                        _previousPlayer.PLAYER.NOTETOSUBTRACT = _previousPlayer.PLAYER.NOTETOSUBTRACT + "Chat Doi Heo;";
                        _currentPlayer.PLAYER.NOTETOADD = _currentPlayer.PLAYER.NOTETOADD + "Chat Doi Heo;";

                        if (Check4CardsOfKind(_arrCurrentCards))
                        {
                            INS_VNPockerStage.sLoseRate *= 3;
                        }
                        else if (CheckPine(_arrCurrentCards) && _arrCurrentCards.Count > 6)
                        {
                            INS_VNPockerStage.sLoseRate *= 4;
                        }
                        else
                        {
                            INS_VNPockerStage.sLoseRate *= 2;
                        }

                        return true;
                    }
                }
            }
            else
            {
                if (CheckSortedArray(_arrPreviousCards))
                {
                    if (CheckSortedArray(_arrCurrentCards) && _arrCurrentCards.Count == _arrPreviousCards.Count
                        && ArrayUtil.Max(_arrCurrentCards) > ArrayUtil.Max(_arrPreviousCards))
                    {
                        return true;
                    }
                }

                if (_arrPreviousCards.Count % 2 == 0) //4, 6, 8, 10, 12 quan bai
                {
                    if (CheckPine(_arrPreviousCards))
                    {
                        if (CheckPine(_arrCurrentCards))
                        {
                            if (_arrCurrentCards.Count > _arrPreviousCards.Count)
                            {
                                _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                                _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                                _previousPlayer.PLAYER.NOTETOSUBTRACT = _previousPlayer.PLAYER.NOTETOSUBTRACT + "Chat Doi Thong;";
                                _currentPlayer.PLAYER.NOTETOADD = _currentPlayer.PLAYER.NOTETOADD + "Chat Doi Thong;";
                                INS_VNPockerStage.sLoseRate *= 2;
                                return true;
                            }
                            else if (_arrCurrentCards.Count == _arrPreviousCards.Count)
                            {
                                if (ArrayUtil.Max(_arrCurrentCards) > ArrayUtil.Max(_arrPreviousCards))
                                {
                                    _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                                    _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                                    _previousPlayer.PLAYER.NOTETOSUBTRACT = _previousPlayer.PLAYER.NOTETOSUBTRACT + "Chat Doi Thong;";
                                    _currentPlayer.PLAYER.NOTETOADD = _currentPlayer.PLAYER.NOTETOADD + "Chat Doi Thong;";
                                    INS_VNPockerStage.sLoseRate *= 2;
                                    return true;
                                }
                            }
                        }
                        else if(Check4CardsOfKind(_arrCurrentCards)&&_arrPreviousCards.Count == 6)
                        {
                            _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            _previousPlayer.PLAYER.NOTETOSUBTRACT = _previousPlayer.PLAYER.NOTETOSUBTRACT + "Chat Doi Thong;";
                            _currentPlayer.PLAYER.NOTETOADD = _currentPlayer.PLAYER.NOTETOADD + "Chat Doi Thong;";
                            INS_VNPockerStage.sLoseRate *= 3;
                            return true;
                        }
                    }
                    else if (Check4CardsOfKind(_arrPreviousCards))
                    {
                        if ((Check4CardsOfKind(_arrCurrentCards) && (((INS_Card)_arrCurrentCards[0]).CARD.CARDVALUE > 
                                                           ((INS_Card)_arrPreviousCards[0]).CARD.CARDVALUE)) 
                            || ((CheckPine(_arrCurrentCards) && (_arrCurrentCards.Count > 6))))
                        {
                            _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                            _previousPlayer.PLAYER.NOTETOSUBTRACT = _previousPlayer.PLAYER.NOTETOSUBTRACT + "Chat Tu Qui;";
                            _currentPlayer.PLAYER.NOTETOADD = _currentPlayer.PLAYER.NOTETOADD + "Chat Tu Qui;";
                            INS_VNPockerStage.sLoseRate *= 4;
                            return true;
                        }
                    }
                }
                else //3, 5, 7, 9, 11 quan bai
                {
                    if (Check3CardsOfKind(_arrPreviousCards))
                    {
                        if (Check3CardsOfKind(_arrCurrentCards) &&
                            ArrayUtil.Max(_arrCurrentCards) > ArrayUtil.Max(_arrPreviousCards))
                        {
                            if(CheckPigs(_arrCurrentCards))
                            {
                                for(int i=0;i<_arrCurrentCards.Count;i++){
                                    INS_VNPockerStage.sLoseRate += ((INS_Card)_arrCurrentCards[i]).CARD.CARDTYPE - 48;
                                }
                            }
                            
                            return true;
                        }
                        else if (CheckPigs(_arrPreviousCards))
                        {
                            if(CheckPine(_arrCurrentCards) && (_arrCurrentCards.Count >= 10))
                            {
                                _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY = _previousPlayer.PLAYER.AMOUNT_DECREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                                _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY = _currentPlayer.PLAYER.AMOUNT_INCREASE_MONEY + INS_VNPockerStage.sBetMoney * INS_VNPockerStage.sLoseRate;
                                _previousPlayer.PLAYER.NOTETOSUBTRACT = _previousPlayer.PLAYER.NOTETOSUBTRACT + "Chat 3 Heo;";
                                _currentPlayer.PLAYER.NOTETOADD = _currentPlayer.PLAYER.NOTETOADD + "Chat 3 Heo;";
                                INS_VNPockerStage.sLoseRate *= 5;
                                return true;
                            }
                        }
                    }
                }
            }

            return false;
        }

        public static Boolean CheckSortedArray(ArrayList _array)
        {
            if(ArrayUtil.CheckDESCArray(_array) || ArrayUtil.CheckASCArray(_array))
            {
                return true;
            }
            return false;
        }

        public static Boolean CheckPine(ArrayList _array)
        {
            if ((_array.Count % 2 != 0) || (_array.Count < 6) || (_array.Count > 12))
            {
                return false;
            }
            int i = 0;
            for(i=0;i<_array.Count - 3;i++)
            {
                if(i%2 == 0)
                {
                    if((((INS_Card)_array[i]).CARD.CARDVALUE != ((INS_Card)_array[i+1]).CARD.CARDVALUE) ||
                       (((INS_Card)_array[i]).CARD.CARDVALUE - ((INS_Card)_array[i + 2]).CARD.CARDVALUE != 1))
                    {
                        return false;
                    }
                }
            }

            i++;
            if (((INS_Card)_array[i]).CARD.CARDVALUE !=
                        ((INS_Card)_array[i + 1]).CARD.CARDVALUE)
            {
                return false;
            }
            return true;
        }

        public static Boolean CheckACouple(ArrayList _array)
        {
            if(_array.Count != 2){
                return false;
            }
            if (((INS_Card)_array[0]).CARD.CARDVALUE 
                != ((INS_Card)_array[1]).CARD.CARDVALUE)
            {
                return false;
            }
            return true;
        }

        //Kiem tra 3 quan cung loai (eg. 333)
        public static Boolean Check3CardsOfKind(ArrayList _array)
        {
            if (_array.Count != 3)
            {
                return false;
            }
            if (((INS_Card)_array[0]).CARD.CARDVALUE == ((INS_Card)_array[1]).CARD.CARDVALUE &&
                ((INS_Card)_array[1]).CARD.CARDVALUE == ((INS_Card)_array[2]).CARD.CARDVALUE)
            {
                return true;
            }
            return false;
        }

        //Kiem tra tu qui, eg. 4444
        public static Boolean Check4CardsOfKind(ArrayList _array)
        {
            if (_array.Count != 4)
            {
                return false;
            }
            if (((INS_Card)_array[0]).CARD.CARDVALUE == ((INS_Card)_array[1]).CARD.CARDVALUE &&
                ((INS_Card)_array[1]).CARD.CARDVALUE == ((INS_Card)_array[2]).CARD.CARDVALUE &&
                ((INS_Card)_array[2]).CARD.CARDVALUE == ((INS_Card)_array[3]).CARD.CARDVALUE)
            {
                return true;
            }
            return false;
        }        

        public static Boolean CheckPigs(ArrayList _array)
        {
            for(int i=0;i<_array.Count;i++)
            {
                if(((INS_Card)_array[i]).CARD.CARDVALUE != Constants.GT_HEO)
                {
                    return false;
                }
            }
            return true;
        }

        public static Boolean CheckWin(INS_Player _nguoichoi)
        {
            for (int i = 0; i < 13; i++)
            {
                if (!_nguoichoi[i].CARD.PLAY)
                    return false;
            }
            return true;
        }
    }
}
